package com.augmented.jellybean.util;

import android.content.Context;
import android.content.Intent;
import android.database.DataSetObserver;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import com.augmented.jellybean.CategoryListActivity;
import com.augmented.jellybean.R;

public class ExpandableCustomAdapter extends BaseExpandableListAdapter implements OnGroupExpandListener,
OnGroupClickListener {
	private LayoutInflater inflater;
	private ArrayList<ExpandableParent> mParent;

	public ExpandableCustomAdapter(Context context, ArrayList<ExpandableParent> parent){
		mParent = parent;
		inflater = LayoutInflater.from(context);
	}


	@Override
	//counts the number of group/parent items so the list knows how many times calls getGroupView() method
	public int getGroupCount() {
		return mParent.size();
	}

	@Override
	//gets the title of each parent/group
	public Object getGroup(int i) {
		return mParent.get(i).getTitle();
	}


	//gets the name of each item
	public Bitmap getChild(int i) {
		return mParent.get(i).getImg();
	}

	@Override
	public long getGroupId(int i) {
		return i;
	}

	@Override
	public long getChildId(int i, int i1) {
		return i1;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	//in this method you must set the text to see the parent/group on the list
	public View getGroupView(int i, boolean b, View view, final ViewGroup viewGroup) {

		if (view == null) {
			view = inflater.inflate(R.layout.augmented_jellybean_list_item_parent, viewGroup,false);
		}

		TextView textView = (TextView) view.findViewById(R.id.list_item_text_view);
		//"i" is the position of the parent/group in the list
		textView.setText(getGroup(i).toString());
		textView.setFocusable(false);
		
		Button btnRedirect	 = (Button) view.findViewById(R.id.parentButton);
		btnRedirect.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				// TODO
				
			}
		});
		
		return view;
	}

	@Override
	//in this method you must set the text to see the children on the list
	public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
		if (view == null) {
			view = inflater.inflate(R.layout.augmented_jellybean_list_item_child, viewGroup,false);
		}

		ImageView imageView = (ImageView) view.findViewById(R.id.list_item_text_child);
		imageView.setImageBitmap(mParent.get(i).getImg());

		// TextView textView = (TextView) view.findViewById(R.id.list_item_text_child);
		//"i" is the position of the parent/group in the list and 
		//"i1" is the position of the child
		// textView.setText(mParent.get(i).getArrayChildren().get(i1));

		//return the entire view
		return view;
	}

	@Override
	public boolean isChildSelectable(int i, int i1) {
		return true;
	}

	@Override
	public void registerDataSetObserver(DataSetObserver observer) {
		/* used to make the notifyDataSetChanged() method work */
		super.registerDataSetObserver(observer);
	}


	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return 0;
	}


	@Override
	public void onGroupExpand(int groupPosition) {
		// TODO Auto-generated method stub

		View view = inflater.inflate(R.layout.augmented_jellybean_list_item_child, null);

		ImageView imageView = (ImageView) view.findViewById(R.id.list_item_text_child);
		imageView.setImageBitmap(mParent.get(groupPosition).getImg());
	}


	public void setOnChildClickListener(
			OnChildClickListener onChildClickListener) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public boolean onGroupClick(ExpandableListView parent, View v,
			int groupPosition, long id) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.augmented_jellybean_list_item_child, null);

		ImageView imageView = (ImageView) view.findViewById(R.id.list_item_text_child);
		imageView.setImageBitmap(mParent.get(groupPosition).getImg());
		
		return false;
	}
}
